package com.simtop.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.simtop.common.ServerResponse;

import com.simtop.pojo.User;
import com.simtop.pojo.exchange.Question;
import com.simtop.pojo.wddata.MainTain;
import com.simtop.pojo.wddata.WdDate;
import com.simtop.pojo.wddata.api;
import com.simtop.service.ApiService;
import com.simtop.util.JwtUtil;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/device")
@CrossOrigin
public class DeviceController {

    @Autowired
    private ApiService apiService;

    //获取维护
    @PostMapping("/viewData")
    public ServerResponse rtdata(@RequestBody String data,HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        try {
            JSONObject jsonObject = JSONObject.parseObject(data);
            Integer pageNum = jsonObject.getInteger("pageNum");
            Integer pageSize = jsonObject.getInteger("pageSize");
            if(StringUtils.isEmpty(pageNum) || pageNum == 0){
                pageNum = 1;
            }
            if(StringUtils.isEmpty(pageSize) || pageSize == 0){
                pageSize = 10;
            }
            PageHelper.startPage(pageNum,pageSize);
            //查询表中所有数据
            List<MainTain> findAlldeviceList= apiService.findAlldeviceList();

            //核心 拿到每一个Id 查询到最新的状态值

            for (MainTain fidDevice:findAlldeviceList
                 ) {
                int deviceId = fidDevice.getDeviceId();
                String testType = fidDevice.getTestType();
                //找到值value
                WdDate newWdDateValue = apiService.findDeviceValue(deviceId,testType);
                //核心 设备值
                Float val = newWdDateValue.getVal();
                System.out.println(val);
                //找到状态值value
                //System.out.println(val);
                WdDate newWdDateStatus = apiService.findDeviceStatusValue(deviceId);
                //状态值
                float val1 = newWdDateStatus.getVal();
                //System.out.println(val1);


                int deviceValue  = (int) val1;
                fidDevice.setDeviceStatus(deviceValue);
                fidDevice.setDeviceValue(val);



            }
            PageInfo<MainTain> pageInfo = new PageInfo<MainTain>(findAlldeviceList);
            return ServerResponse.createBySuccess("获取内容成功",pageInfo);
        }
        catch (Exception e){
            e.printStackTrace();
            return ServerResponse.createByError();
        }
    }

    //修改设备维护
    @PostMapping("/upDateViewData")
    public ServerResponse upDateViewData(@RequestBody String data,HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        try {
            JSONObject jsonObject = JSONObject.parseObject(data);
            JSONArray devicepolice = jsonObject.getJSONArray("devicepolice");
            String devicepolicelist = String.valueOf(devicepolice);
            Integer deviceId = jsonObject.getInteger("deviceId");
            String testLocation = jsonObject.getString("testLocation");
            Integer deviceNormalup = jsonObject.getInteger("deviceNormalup");
            Integer devicePolicedown = jsonObject.getInteger("devicePolicedown");
            System.out.println(devicepolice);
            System.out.println(testLocation);
            System.out.println(deviceNormalup);
            System.out.println(devicePolicedown);
            //更新
            apiService.updateDevice(deviceId,devicepolicelist,testLocation,deviceNormalup,devicePolicedown);

            return ServerResponse.createBySuccess("修改成功");
        }
        catch (Exception e){
            e.printStackTrace();
            return ServerResponse.createByError();
        }
    }


}
